home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / Zoomed Video Driver v1.0 SDK / Tools / PC Card DispNameReg / Src / SaveNameRegistry.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-02  |  9.2 KB  |  336 lines  |  [TEXT/CWIE]

  1. #include "SystemSoft.h"
  2. /*
  3.  * SaveNameRegistry.c
  4.  * Copyright © 1995, Apple Computer Inc. All Rights Reserved.
  5.  */
  6.  
  7. #include "DisplayNameRegistry.h"
  8.  
  9. #define kEndOfLine                0x0D    /* Return            */
  10. #define TestTDFlag(elementHdl, mask)    (((**elementHdl).flag & (mask)) != 0)
  11.  
  12. /*
  13.  * Standard File dialog items. This follows the layout of the old Standard File
  14.  * (-3999) DITL. Our private items are at the end.
  15.  */
  16. enum {                                        /* Save File Dialog                */
  17.     kSaveButton = 1,
  18.     kDontSaveButton,
  19.     kSaveAsButton,
  20.     kFolderHierarchyPopup,
  21.     kEjectButton,
  22.     kDriveButton,
  23.     kFileNameText,
  24.     kCatalogList,
  25.     kSaveAllRadio,
  26.     kSaveVisibleRadio,
  27.     
  28.     kFileCreator    =    'R*ch'    ,        // BBEdit rules
  29.     kFileType        =    'TEXT'    
  30. };
  31.  
  32. OSErr WriteVisibleNameRegistry(TwistDownHdl twistDownHandle);
  33. OSErr WriteEntireNameRegistry(TwistDownHdl twistDownHandle);
  34. OSErr WriteThisElement(TwistDownHdl twistDownHandle);
  35. void SaveFileDialog(short dialogID, ConstStr255Param promptString,
  36.         ConstStr255Param originalName, SFReply * reply);
  37. static pascal short PutDialogFilter(short itemHit, DialogPtr theDialog);
  38. static void    SelectDialogRadio(DialogPtr theDialog, short selectedButton);
  39. /* Debug only */
  40. void CheckForNullByte(Ptr dataPtr, long dataLength);
  41.  
  42. //--------------------------------------------------------------------------------
  43. //    Create an output file. .
  44. void CreateOutputFile(void)
  45. {
  46.     OSErr                    status, closeStatus;
  47.     TwistDownHdl            listHead;
  48.     Cell                    theCell;
  49.     
  50.     SaveFileDialog(DLOG_SFPutFile, "\pOutput File", "\pName Registry Output", &gSFReply);
  51.     if (gSFReply.good == FALSE)
  52.         status = userCanceledErr;
  53.     else
  54.     {
  55.         // Create the file, eliminating any duplicate.
  56.         SetCursor(*GetCursor(watchCursor));
  57.         status = Create(gSFReply.fName, gSFReply.vRefNum, kFileCreator, kFileType);
  58.  
  59.         if (status == dupFNErr)
  60.         {    // Exists already?
  61.             status = FSDelete(gSFReply.fName, gSFReply.vRefNum);
  62.             if (status == noErr)
  63.                 status = Create(gSFReply.fName, gSFReply.vRefNum, kFileCreator, kFileType);
  64.         }
  65.  
  66.         if (status == noErr)
  67.             status = FSOpen(gSFReply.fName, gSFReply.vRefNum, &gSaveFileRefNum);
  68.  
  69.         if (status != noErr)
  70.         {
  71.             if (status != userCanceledErr)
  72.                 NonFatalError(status, "\pCan't create file");
  73.         } else
  74.         {
  75.             SetPt(&theCell, 0, 0);
  76.             listHead = GetTwistDownElementHandle(gCurrentBrowserPtr->theList, theCell);
  77.             if (gSaveAllElements)
  78.                 status = WriteEntireNameRegistry(listHead);
  79.             else
  80.                 status = WriteVisibleNameRegistry(listHead);
  81.  
  82.             if (status != noErr)
  83.                 NonFatalError(status, "\pCan't write file");
  84.  
  85.             closeStatus = FSClose(gSaveFileRefNum);
  86.             if (status == noErr && closeStatus != noErr)
  87.             {
  88.                 NonFatalError(closeStatus, "\pCan't close file");
  89.                 status = closeStatus;
  90.             }
  91.  
  92.             closeStatus = FlushVol(NULL, gSFReply.vRefNum);
  93.             if (status == noErr && closeStatus != noErr)
  94.                 NonFatalError(closeStatus, "\pCan't flush volume");
  95.         }
  96.         InitCursor();
  97.     }
  98. }
  99.  
  100. #ifdef SYSF
  101.  
  102. #include <stdio.h>
  103. #include <Strings.h>
  104.  
  105. #define FILENAMESTUB    "NameRegSnap"
  106.  
  107. // this a Real Hack
  108. Boolean        gIgnoreUntilFirstCardEntry;
  109.  
  110. //--------------------------------------------------------------------------------
  111. // find an unused filename and create a text file that will be used in the snapshot
  112. // the file will be created in the current directory
  113. OSErr GetOutFileName(Str255 outFileName);
  114. OSErr GetOutFileName(Str255 outFileName)
  115. {
  116.     OSErr                    status = dupFNErr;
  117.     short                    vRefNum = 0;
  118.     static short            uniqueID = 0;
  119.     
  120.     while (status != noErr)
  121.     {
  122.         sprintf((char *) outFileName, "%s%d", FILENAMESTUB, uniqueID);
  123.         c2pstr((char *) outFileName);
  124.         
  125.         status = Create(outFileName, vRefNum, kFileCreator, kFileType);
  126.         uniqueID++;
  127.     }
  128.     
  129.     return status;    
  130. }
  131.  
  132.  
  133. //--------------------------------------------------------------------------------
  134. //    Write a snapshot of the current state of the registry window to a disk file
  135. void SnapshotToDisk(Boolean isExpanded, Boolean noSockets, char *fileName)
  136. {
  137.     OSErr                    status = noErr, ignore, closeStatus;
  138.     TwistDownHdl            listHead;
  139.     Cell                    theCell;
  140.     Str255                    outFileName;
  141.     short                    vRefNum = 0;
  142.     
  143.     // open a file
  144.     if (strlen(fileName) == 0)
  145.         status = GetOutFileName(outFileName);
  146.     else
  147.     {
  148.         strcpy((char *) outFileName, fileName);
  149.         c2pstr((char *) outFileName);
  150.         ignore = FSDelete(outFileName,vRefNum);
  151.         ignore = Create(outFileName, vRefNum, kFileCreator, kFileType);
  152.     }
  153.     
  154.     if (status == noErr)
  155.         status = FSOpen(outFileName, vRefNum, &gSaveFileRefNum);
  156.  
  157.     // set a global Boolean - HACK
  158.     gIgnoreUntilFirstCardEntry = noSockets;
  159.     
  160.     // scan the list and write out the registry entries
  161.     SetPt(&theCell, 0, 0);
  162.     listHead = GetTwistDownElementHandle(gCurrentBrowserPtr->theList, theCell);
  163.     if (isExpanded)
  164.         status = WriteEntireNameRegistry(listHead);
  165.     else
  166.         status = WriteVisibleNameRegistry(listHead);
  167.  
  168.     if (status != noErr)
  169.         NonFatalError(status, "\pCan't write file");
  170.  
  171.     // all done, close the file
  172.     closeStatus = FSClose(gSaveFileRefNum);
  173.     if (status == noErr && closeStatus != noErr)
  174.     {
  175.         NonFatalError(closeStatus, "\pCan't close file");
  176.         status = closeStatus;
  177.     }
  178.  
  179.     // all done, really make sure that it is closed
  180.     closeStatus = FlushVol(NULL, vRefNum);
  181.     if (status == noErr && closeStatus != noErr)
  182.         NonFatalError(closeStatus, "\pCan't flush volume");
  183. }
  184. #endif
  185.  
  186. //--------------------------------------------------------------------------------
  187. OSErr WriteVisibleNameRegistry(TwistDownHdl twistDownHandle)
  188. {
  189.     OSErr                    status;
  190.  
  191.     status = noErr;
  192.     while (status == noErr && twistDownHandle != NULL)
  193.     {
  194.         status = WriteThisElement(twistDownHandle);
  195.         if (status == noErr
  196.          && (**twistDownHandle).subElement != NULL
  197.          && TestTDFlag(twistDownHandle, kShowSublist))
  198.             status = WriteVisibleNameRegistry((**twistDownHandle).subElement);
  199.         twistDownHandle = (**twistDownHandle).nextElement;
  200.     }
  201.     return (status);
  202. }
  203.  
  204. //--------------------------------------------------------------------------------
  205. OSErr WriteEntireNameRegistry(TwistDownHdl twistDownHandle)
  206. {
  207.     OSErr                        status;
  208.     
  209.     status = noErr;
  210.     while (status == noErr && twistDownHandle != NULL)
  211.     {
  212.         status = WriteThisElement(twistDownHandle);
  213.         if (status == noErr && (**twistDownHandle).subElement != NULL)
  214.             status = WriteEntireNameRegistry((**twistDownHandle).subElement);
  215.         twistDownHandle = (**twistDownHandle).nextElement;
  216.     }
  217.     return (status);
  218. }
  219.  
  220. //--------------------------------------------------------------------------------
  221. //    Write one line of text to the output file.
  222. OSErr WriteThisElement(TwistDownHdl twistDownHdl)
  223. {
  224.     OSErr                        status;
  225.     long                        textLength = 1;
  226.     short                        handleState;
  227.     static char                    gEndOfLine[1] = { kEndOfLine };
  228. #define ELEM (**twistDownHdl)
  229.  
  230.     handleState = HGetState((Handle) twistDownHdl);
  231.     HLock((Handle) twistDownHdl);
  232. #ifdef SYSF
  233.     if (gIgnoreUntilFirstCardEntry)
  234.     {
  235.         if (strncmp(WECARECARD, (const char *) ELEM.data, strlen(WECARECARD)) != 0)    // "Devices:device-tree:bandit:ti1130:"
  236.             return (noErr);
  237.         // we have skipped all the sockets now start listing elements
  238.         gIgnoreUntilFirstCardEntry = false;
  239.     }
  240.  
  241.     // put a blank line before every top level registry entry
  242.     if (ELEM.indentLevel == 0)
  243.         status = FSWrite(gSaveFileRefNum, &textLength, gEndOfLine);
  244. #endif
  245.     textLength = ELEM.dataLength;
  246.     if (textLength == 0)
  247.         status = noErr;
  248.     else {
  249.         CheckForNullByte((Ptr) ELEM.data, ELEM.dataLength);
  250.         status = FSWrite(gSaveFileRefNum, &textLength, ELEM.data);
  251.     }
  252.     HSetState((Handle) twistDownHdl, handleState);
  253.     if (status == noErr) {
  254.         textLength = 1;
  255.         status = FSWrite(gSaveFileRefNum, &textLength, gEndOfLine);
  256.     }
  257.     if (status != noErr)
  258.         NonFatalError(status, "\pWriteThisElement");
  259.     return (status);
  260. }
  261.  
  262. //--------------------------------------------------------------------------------
  263. void SaveFileDialog(short dialogID, ConstStr255Param promptString,
  264.             ConstStr255Param originalName, SFReply * reply)
  265. {
  266.     static DlgHookUPP        dlgHookUPP = NULL;
  267.     Point                    where;
  268.  
  269.     SetPt(&where, 80, 80);
  270.     if (dlgHookUPP == NULL)
  271.         dlgHookUPP = NewDlgHookProc(PutDialogFilter);
  272.     SFPPutFile(
  273.         where,
  274.         promptString,
  275.         originalName,
  276.         dlgHookUPP,
  277.         reply,
  278.         dialogID, NULL);
  279. }
  280.  
  281. //--------------------------------------------------------------------------------
  282. static pascal short PutDialogFilter(short itemHit, DialogPtr theDialog)
  283. {
  284.     short                    itemType;
  285.     Handle                    itemHandle;
  286.     Rect                    itemRect;
  287.  
  288.     switch (itemHit)
  289.     {
  290.         case sfHookFirstCall:
  291.             SelectDialogRadio(theDialog, (gSaveAllElements) ? kSaveAllRadio : kSaveVisibleRadio);
  292.             break;
  293.  
  294.         case kSaveAllRadio:
  295.         case kSaveVisibleRadio:
  296.             SelectDialogRadio(theDialog, itemHit);
  297.             break;
  298.  
  299.         case kSaveButton:
  300.             GetDialogItem(theDialog, kSaveAllRadio, &itemType, &itemHandle, &itemRect);
  301.             gSaveAllElements = GetControlValue((ControlHandle) itemHandle);
  302.         }
  303.     return (itemHit);
  304. }        
  305.  
  306.  
  307. //--------------------------------------------------------------------------------
  308. static void SelectDialogRadio(DialogPtr theDialog, short selectedButton)
  309. {
  310.     short                    itemType, i;
  311.     Handle                    itemHandle;
  312.     Rect                    itemRect;
  313.  
  314.     for (i = kSaveAllRadio; i <= kSaveVisibleRadio; i++)
  315.     {
  316.         GetDialogItem(theDialog, i, &itemType, &itemHandle, &itemRect);
  317.         SetControlValue((ControlHandle) itemHandle, (i == selectedButton) ? 1 : 0);
  318.     }
  319. }
  320.  
  321. //--------------------------------------------------------------------------------
  322. void CheckForNullByte(Ptr dataPtr, long dataLength)
  323. {
  324.     long                    i;
  325.     
  326.     for (i = 0; i < dataLength; i++)
  327.     {
  328.         if (dataPtr[i] == 0)
  329.         {
  330.             printf("%ld in %ld \"%.*s\"\n", i, dataLength, dataLength, dataPtr);
  331.             break;
  332.         }
  333.     }
  334. }
  335.  
  336.